package leetcode.part1;

import org.junit.Test;

/**
 * 反转字符串中的单词
 */
public class S151 {
    public String reverseWords(String s) {
        StringBuilder el = new StringBuilder();
        StringBuilder result = new StringBuilder();
        boolean flag=true;
        for (int i = s.length() - 1; i >= 0; i--) {
            if (s.charAt(i) == ' '&&flag) {
                el.reverse();
                result.append(el);
                result.append(" ");
                el.setLength(0);
                flag=false;

            }else if (s.charAt(i)!=' '){
                el.append(s.charAt(i));
                flag=true;
            }

        }
        el.reverse();
        result.append(el);
        return result.toString().trim();

    }

    @Test
    public void test() {
        String s = "  hello world  ";
        System.out.println(reverseWords(s));
    }
}
